package com.unirio.pm.passwordchecker;

import com.unirio.pm.interfaces.PasswordChecker;
import com.unirio.pm.interfaces.PasswordRule;
import com.unirio.pm.rules.RegexChecker;
import com.unirio.pm.rules.ScoreEvaluator;

public class PasswordChecker01 implements PasswordChecker{
	
	private PasswordRule[] rules;
	
	public PasswordChecker01() {
		initializeRules();
	}

    public void checkPassword(String password){
        int score = returnPasswordScore(password);
        String evaluation = returnPasswordEvaluation(score);
        System.out.println("Strength score is: " + score);
        System.out.println("Strength verdict: " + evaluation);
    } 
    
    public void initializeRules() {
    	rules = new PasswordRule[12];
    	rules[0] = new RegexChecker("^.{0,4}$", 3);
    	rules[1] = new RegexChecker("^.{5,7}$", 6);
    	rules[2] = new RegexChecker("^.{8,16}$", 12);
    	rules[3] = new RegexChecker("^.{17,}$", 18);
    	rules[4] = new RegexChecker("atLeastOneUpperCase",".*[A-Z].*",5);
    	rules[5] = new RegexChecker("atLeastOneLowerCase",".*[a-z].*", 1);
    	rules[6] = new RegexChecker("atLeastThreeDigits",".*[0-9].*[0-9].*[0-9].*", 5);
    	rules[7] = new RegexChecker("containsOneSpecialCharacter", "\\w*\\W\\w*", 5);
    	rules[8] = new RegexChecker("containsTwoOrMoreSpecialCharacters",".*\\W.*\\W.*", 10);
    	rules[9] = new RegexChecker("atLeastOneUpperAndLowerCase","^(?=.*[A-Z].*)(?=.*[a-z].*).+$", 2);
    	rules[10] = new RegexChecker("atLeastOneLetterAndNumber","^(?=.*[A-Za-z].*)(?=.*[0-9].*).+$", 2);
    	rules[11] = new RegexChecker("atLeastOneSpecialCharacterAndWordCharacter","^(?=.*\\W.*)(?=.*\\w.*).+$", 2);
    }
    
    public int returnPasswordScore(String password) {
    	int score = 0;
    	for(int i=0; i<rules.length; i++) {
    		score += rules[i].checkRule(password);
    	}
    	return score;
    }
    
    public String returnPasswordEvaluation(int score) {
    	int[] range = {16,25,35,45};
    	String[] result = {"very weak", "weak", "medium", "strong", "very strong"};
    	ScoreEvaluator evaluator = new ScoreEvaluator(range, result);
    	return evaluator.evaluate(score);
    }
   
}
